<?php
declare (strict_types=1);

namespace app\admin\controller\tally;

use app\admin\controller\CommonController;
use app\common\model\AclUser;
use app\common\model\TallyCustom;
use app\common\model\TallyProgressMonth;
use app\common\model\TallyTodo;
use ffhome\frame\service\DictDataService;
use think\db\BaseQuery;

class ProgressMonth extends CommonController
{
    protected $modelName = TallyProgressMonth::MODEL_NAME;
    protected $alias = 'm';

    protected function buildWhere(array $param): array
    {
        if (!empty($param['m_progress_month_ge'])) {
            $param['m_progress_month_ge'] = $param['m_progress_month_ge'] . '-01';
        }
        if (!empty($param['m_progress_month_le'])) {
            $param['m_progress_month_le'] = $param['m_progress_month_le'] . '-01';
        }
        $where = parent::buildWhere($param);
        if (auth('roleAccounting')) {
            $where[] = ["{$this->alias}.accounting_id", '=', currentUserId()];
        }
        return $where;
    }

    protected function indexPage($param)
    {
        $param['m_progress_month_ge'] = date('Y-m');
        return parent::indexPage($param);
    }

    protected function getBaseModel(): BaseQuery
    {
        return parent::getBaseModel()
            ->leftJoin(AclUser::MODEL_NAME . ' a', "a.id={$this->alias}.accounting_id")
            ->leftJoin(TallyCustom::MODEL_NAME . ' c', "c.id={$this->alias}.custom_id");
    }

    protected function getSearchFields(): string
    {
        $prefix = config('database.connections.' . config('database.default') . '.prefix');
        $tabTodo = $prefix . TallyTodo::MODEL_NAME;
        return "{$this->alias}.*,DATE_FORMAT({$this->alias}.progress_month,'%Y-%m') _month"
            . ",concat(a.username,a.nickname) accounting_name,c.name custom_name"
            . ",ifnull((select sum(t.actual_time) from {$tabTodo} t where t.month_id={$this->alias}.id),0) actual_time";
    }

    protected function onBeforeDelete($id): array
    {
        // 判断存在日工作表
        if (TallyTodo::findByMonthId($id)) {
            $this->error(lang('month.error_exist_todo'));
        }
        return parent::onBeforeDelete($id);
    }

    protected function getExportHeader()
    {
        return [
            [lang('common.month'), '_month'],
            [lang('custom.name'), 'custom_name'],
            [lang('common.estimate_time_alias'), 'estimate_time'],
            [lang('common.actual_time_alias'), 'actual_time'],
            [lang('month.receipt'), 'receipt'],
            [lang('month.make_account'), 'make_account'],
            [lang('month.report_forms'), 'report_forms'],
            [lang('month.taxes'), 'taxes'],
            [lang('month.voucher_binding'), 'voucher_binding'],
            [lang('month.deduction_binding'), 'deduction_binding'],
            [lang('month.other'), 'other'],
            [lang('common.appraise'), 'appraise'],
            [lang('month.remark'), 'remark'],
            [lang('accounting.name'), 'accounting_name'],
            [lang('common.update_time'), 'update_time'],
        ];
    }

    protected function convertExportData(&$list)
    {
        foreach ($list as &$vo) {
            $vo['receipt'] = DictDataService::getNameByValue($vo['receipt'], 'receipt_list');
            $vo['make_account'] = DictDataService::getNameByValue($vo['make_account'], 'make_account_list');
            $vo['report_forms'] = DictDataService::getNameByValue($vo['report_forms'], 'report_forms_list');
            $vo['taxes'] = DictDataService::getNameByValue($vo['taxes'], 'taxes_list');
            $vo['voucher_binding'] = DictDataService::getNameByValue($vo['voucher_binding'], 'voucher_binding_list');
            $vo['deduction_binding'] = DictDataService::getNameByValue($vo['deduction_binding'], 'deduction_binding_list');
            $vo['other'] = DictDataService::getNameByValue($vo['other'], 'other_list');
            $vo['appraise'] = DictDataService::getNameByValue($vo['appraise'], 'appraise_list');
        }
    }

    public function generate()
    {
        TallyProgressMonth::generate(date('Y-m-01'));
        $nextMonth = date('Y-m-d', strtotime(date('Y-m-01') . ' +1 month'));
        TallyProgressMonth::generate($nextMonth);
        $this->success(lang('month.generate_success_info'));
    }
}
